package com.sunshine.lnuplus.ui.activity

import android.content.Context
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.KeyEvent
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.base.BaseActivity
import com.sunshine.lnuplus.callback.TimetableSettingCallBack
import com.sunshine.lnuplus.model.InitData
import com.sunshine.lnuplus.model.LessonBean
import com.sunshine.lnuplus.utils.*
import com.sunshine.lnuplus.view.TimetableSettingView
import com.sunshine.lnuplus.viewModel.SplashViewModel
import kotlinx.android.synthetic.main.activity_timetable_setting.*

class TimetableSettingActivity : BaseActivity() {

    private lateinit var lessonList: ArrayList<LessonBean>
    private lateinit var viewModel: SplashViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_timetable_setting)

        viewModel = ViewModelProvider(this).get(SplashViewModel::class.java)
        StatusBarUtils.setStatusBarWithColor(this, color)
        initView()
        initTheme()
    }

    private fun initTestTimetable() {
        lessonList = ArrayList<LessonBean>().apply {
            add(LessonBean("1", "测试课程1", 1, 1, 1, "教室", "老师", "0", arrayListOf(1)))
            add(LessonBean("2", "测试课程2", 2, 1, 1, "教室", "老师", "0", arrayListOf(2)))
            add(LessonBean("3", "测试课程3", 3, 1, 2, "教室", "老师", "0", arrayListOf(1)))
            add(LessonBean("4", "测试课程4", 4, 1, 2, "教室", "老师", "0", arrayListOf(2)))
            add(LessonBean("5", "测试课程5", 5, 1, 2, "教室", "老师", "0", arrayListOf(1)))
            add(LessonBean("6", "测试课程6", 6, 1, 1, "教室", "老师", "0", arrayListOf(2)))
            add(LessonBean("7", "测试课程7", 7, 1, 1, "教室", "老师", "0", arrayListOf(1)))
        }
        try {
            setting_timetable_view.init(
                lessonList = lessonList,
                lessonStyle = InitData.lessonStyle,
                thisWeek = 1,
                otherWeek = 1,
                fragmentManager = supportFragmentManager,
                isSetting = true,
                fragment = null,
                adapter = null
            )
        } catch (e: Exception){
            viewModel.getLessonStyle().observe(this, Observer {
                if(getTimetableTheme() == 1){
                    if(getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE).getBoolean("night", false)){
                        it.dateAndTimeTextColor = "#FFFFFF"
                    }else{
                        it.dateAndTimeTextColor = "#000000"
                    }
                }
                setting_timetable_view.init(
                    lessonList = lessonList,
                    lessonStyle = it,
                    thisWeek = 1,
                    otherWeek = 1,
                    fragmentManager = supportFragmentManager,
                    isSetting = true,
                    fragment = null,
                    adapter = null
                )
                viewModel.getLessonStyle().removeObservers(this)
            })
        }

    }

    private fun initView() {
        initTestTimetable()
        title = "课表设置"
        val timetableSettingView = TimetableSettingView()
        timetableSettingView.init(object : TimetableSettingCallBack{
            override fun onChanged() {
                setting_timetable_view.init(
                    lessonList = lessonList,
                    lessonStyle = InitData.lessonStyle,
                    thisWeek = 1,
                    otherWeek = 1,
                    fragmentManager = supportFragmentManager,
                    isSetting = true,
                    fragment = null,
                    adapter = null
                )
            }
        })
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.timetable_setting_view, timetableSettingView)
            .commit()

        getToolBar()?.setNavigationOnClickListener {
            setResult(TagHelper.TIMETABLESETTING)
            finish()
        }
    }

    private fun initTheme() {
        when(getTimetableTheme()){
            1 -> {
                if(isNight){
                    setting_background.setImageDrawable(resources.getDrawable(R.color.primary_black))
                }else{
                    setting_background.setImageDrawable(resources.getDrawable(R.color.white))
                }
            }
            2 -> {
                val bitmap = BitmapFactory.decodeResource(resources, R.mipmap.background)
                setting_background.setImageBitmap(
                    BlurBuilder.blur(
                        this,
                        bitmap,
                        (viewModel.getBackgroundMohu()/100.0*25).toFloat()
                    ))
            }
            3 -> {
                BackgroundUtils.setBackground(
                    this,
                    setting_background,
                    true,
                    viewModel.getBackgroundMohu()
                )
            }
            4 -> {
                BackgroundUtils.setUserBackground(
                    this,
                    setting_background,
                    viewModel.getBackgroundMohu()
                )
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK && event?.action == KeyEvent.ACTION_DOWN) {
            setResult(TagHelper.TIMETABLESETTING)
            finish()
            true
        } else {
            super.onKeyDown(keyCode, event)
        }
    }

    override fun onPause() {
        super.onPause()
        UpdateWidgetUtils.updateWidgets(this)
    }

}
